import com.jaiku.api.JaikuService;
import com.jaiku.api.events.JaikuResultEvent;
import com.jaiku.api.objects.JaikuNote;
import com.jaiku.api.objects.JaikuUser;
import com.jaiku.api.results.JaikuNotesResult;
import com.jaiku.api.results.JaikuUsersResult;


private var jaikuService:JaikuService;

private const _username:String = "imrahil";
private const _personal_key:String = "";

private function init():void
{
	jaikuService = new JaikuService(_username, _personal_key);
	
//	getAuth();
//	getPublicStream();
//	getContactsStream();
//	getUserStream("imrahil");
//	getUserPresence("termie", 36748372);
//	getUserProfile("fourdollars");
//	getChannelStream("Nokia");
//	getChannelPresence("Nokia", 34920515); 
	postPresence();
//	postComment();
}
private function getAuth():void
{
	jaikuService.addEventListener(JaikuResultEvent.ON_VERIFY_AUTH, onGetAuth);
	jaikuService.verifyAuth();
}
private function onGetAuth(evt:JaikuResultEvent):void
{
	trace(evt.data);
}
private function getPublicStream():void
{
	jaikuService.addEventListener(JaikuResultEvent.ON_GET_PUBLIC_STREAM, onGetPublicStream);
	jaikuService.getPublicStream();
}
private function onGetPublicStream(evt:JaikuResultEvent):void
{
	var jaikuNotesResult:JaikuNotesResult = evt.data as JaikuNotesResult;
	var resultArray:Array = jaikuNotesResult.notes as Array;

	output_txt.text += "Results:\n";
	for each(var rawNote:* in resultArray)
	{
		output_txt.text += rawNote + "\n";
	}
}			
private function getContactsStream():void
{
	jaikuService.addEventListener(JaikuResultEvent.ERROR, onFault);
	jaikuService.addEventListener(JaikuResultEvent.ON_GET_CONTACTS_STREAM, onGetContacsStream);
	jaikuService.getContactsStream(true);
}
private function onGetContacsStream(evt:JaikuResultEvent):void
{
	var jaikuNotesResult:JaikuNotesResult = evt.data as JaikuNotesResult;
	var resultArray:Array = jaikuNotesResult.notes as Array;

	output_txt.text += "Results:\n";
	for each(var rawNote:* in resultArray)
	{
		output_txt.text += rawNote + "\n";
	}
}			
private function getUserStream(user:String):void
{
	jaikuService.addEventListener(JaikuResultEvent.ERROR, onFault);
	jaikuService.addEventListener(JaikuResultEvent.ON_GET_USER_STREAM, onGetUserStream);
	jaikuService.getUserStream(user);
}
private function onGetUserStream(evt:JaikuResultEvent):void
{
	var jaikuNotesResult:JaikuNotesResult = evt.data as JaikuNotesResult;
	var resultArray:Array = jaikuNotesResult.notes as Array;

	output_txt.text += "Results:\n";
	for each(var rawNote:* in resultArray)
	{
		output_txt.text += (rawNote as JaikuNote).title + "\n\n";
	}
}
private function getUserPresence (user:String, id:int):void
{
	jaikuService.addEventListener(JaikuResultEvent.ERROR, onFault);
	jaikuService.addEventListener(JaikuResultEvent.ON_GET_PRESENCE, onGetUserPresence);
	jaikuService.getPresence(user, id, false);
}
private function onGetUserPresence(evt:JaikuResultEvent):void
{
	var jaikuNotesResult:JaikuNotesResult = evt.data as JaikuNotesResult;
	var resultArray:Array = jaikuNotesResult.notes as Array;
	output_txt.text += "Results:\n";
	output_txt.text += (resultArray[0] as JaikuNote).url;
}
private function getChannelStream(channel:String):void
{
	output_txt.text = "Request - channelStream\n";
	jaikuService.addEventListener(JaikuResultEvent.ERROR, onFault);
	jaikuService.addEventListener(JaikuResultEvent.ON_GET_CHANNEL_STREAM, onGetChannelStream);
	jaikuService.getChannelStream(channel);
}
private function onGetChannelStream(evt:JaikuResultEvent):void
{
	var jaikuNotesResult:JaikuNotesResult = evt.data as JaikuNotesResult;
	var resultArray:Array = jaikuNotesResult.notes as Array;

	output_txt.text += "Results:\n";
	for each(var rawNote:* in resultArray)
	{
		output_txt.text += (rawNote as JaikuNote).title + " -> " + (rawNote as JaikuNote).created_at_relative + "\n";
	}
}
private function getChannelPresence(channel:String, id:int):void
{
	output_txt.text = "Request - channelPresence\n";
	jaikuService.addEventListener(JaikuResultEvent.ERROR, onFault);
	jaikuService.addEventListener(JaikuResultEvent.ON_GET_PRESENCE, onGetChannelPresence);
	jaikuService.getPresence(channel, id, true);
}
private function onGetChannelPresence(evt:JaikuResultEvent):void
{
	var jaikuNotesResult:JaikuNotesResult = evt.data as JaikuNotesResult;
	var resultArray:Array = jaikuNotesResult.notes as Array;
	output_txt.text += "Results:\n";
	output_txt.text += (resultArray[0] as JaikuNote).title;
	
	var commentsArray:Array = (resultArray[0] as JaikuNote).comments as Array;

	output_txt.text += "\n\nComments:\n";
	for each(var rawNote:* in commentsArray)
	{
		output_txt.text += (rawNote as JaikuNote).content + "\n";
	}
}
private function getUserProfile(user:String):void
{
	output_txt.text = "Request - getUserProfile\n";
	jaikuService.addEventListener(JaikuResultEvent.ERROR, onFault);
	jaikuService.addEventListener(JaikuResultEvent.ON_GET_USER_PROFILE, onGetUserProfile);
	jaikuService.getUserProfile(user);
}
private function onGetUserProfile(evt:JaikuResultEvent):void
{
	var jaikuUsersResult:JaikuUsersResult = evt.data as JaikuUsersResult;
	output_txt.text += "Results:\n";
	output_txt.text += String((jaikuUsersResult.user as JaikuUser).first_name) + "\n";
	output_txt.text += String((jaikuUsersResult.user as JaikuUser).contacts.length);
}
private function postPresence():void
{
	output_txt.text = "Request - postPresence\n";
	jaikuService.addEventListener(JaikuResultEvent.ON_POST_MESSAGE, onPostPresence);
	jaikuService.postPresence('icon nr 300 - test via as3jaikulib');
}
private function onPostPresence(evt:JaikuResultEvent):void
{
	trace('Result - ' + evt.data);
}
private function postComment():void
{
	output_txt.text = "Request - postComment\n";
	jaikuService.addEventListener(JaikuResultEvent.ON_POST_COMMENT, onPostComment);
	jaikuService.postComment('imrahil', 'test comment 4', 36748610, false);
}
private function onPostComment(evt:JaikuResultEvent):void
{
	//trace('Result - ' + evt.data);
}
private function onFault(evt:JaikuResultEvent):void
{
	trace("Error!\n" + evt.data);
}
